package com.project.hqshorttaxi.controller;

import com.github.pagehelper.PageHelper;
import com.project.hqshorttaxi.aop.TagLog;
import com.project.hqshorttaxi.dto.ParkingNodeParam;
import com.project.hqshorttaxi.entity.ParkingNode;
import com.project.hqshorttaxi.service.ParkingNodeService;
import com.project.hqshorttaxi.util.PageInfo;
import com.project.hqshorttaxi.util.RestResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/parkingNode/")
public class ParkingNodeController {

    @Resource
    ParkingNodeService parkingNodeService;

    /**
     * 新增
     */
    @PostMapping("add")
    @TagLog(operation = 2, content = "新增车场车道")
    public RestResult add(@RequestBody ParkingNode param) {
        try {
            parkingNodeService.insert(param);
            return RestResult.ok();
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 更新
     */
    @PostMapping("update")
    @TagLog(operation = 3, content = "更新车场车道")
    public RestResult update(@RequestBody ParkingNode param) {
        try {
            ParkingNode entity = parkingNodeService.getById(param.getId());
            if (entity == null) {
                return RestResult.error("该条数据不存在");
            } else {
                ParkingNodeParam parkingNodeParam = new ParkingNodeParam();
                parkingNodeParam.setParkingNo(param.getParkingNo());
                parkingNodeParam.setInuse(param.getInuse());
                List<ParkingNode> list = parkingNodeService.list(parkingNodeParam);
                for (ParkingNode node : list) {
                    if (node.getInuse() == 0) {
                        node.setInuse(1);
                    } else {
                        node.setInuse(0);
                    }
                    parkingNodeService.update(node);
                }
                parkingNodeService.update(param);
                return RestResult.ok();
            }
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @PostMapping("delete/{id}")
    @TagLog(operation = 4, content = "删除车场车道")
    public RestResult delete(@PathVariable Integer id) {
        try {
            ParkingNode entity = parkingNodeService.getById(id);
            if (entity == null) {
                return RestResult.error("该条数据不存在");
            } else {
                parkingNodeService.delete(id);
                return RestResult.ok();
            }
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 列表
     */
    @PostMapping("page")
    @TagLog(operation = 0, content = "查询车场车道列表")
    public RestResult page(@RequestBody ParkingNodeParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        return RestResult.ok(new PageInfo<>(parkingNodeService.list(param)));
    }

    /**
     * 根据ID获取
     */
    @GetMapping("{id}")
    @TagLog(operation = 0, content = "根据ID获取车场车道详细信息")
    public RestResult pageById(@PathVariable Integer id) {
        return RestResult.ok(parkingNodeService.getById(id));
    }

}
